<?php
/**
 * @copyright  Copyright  LexisNexis, a division of Reed Elsevier Inc. All rights reserved.
 */
//error_reporting(E_ALL);
$startTime = microtime(true);
include_once "UsageClient.php";
$bufferControl = defined('USAGE_BUFFER_CONTROL');

$prePrid = \sdk\UsageClient::instance()->getPrid();
$preCrid = \sdk\UsageClient::instance()->getCrid();

ob_implicit_flush(true);
ob_start(function($buffer) use ($prePrid, $preCrid, $startTime) {
    $headers = headers_list();
    if (!is_array($headers)) {
        \sdk\UsageClient::instance()->log(
            UsageConst::PRODUCT_BASE,
            UsageConst::ACTION_PAGE_VIEW,
            array(
                'code' => 500,
                'execute_time' => microtime(true) - $startTime
            )
        );
        return $buffer;
    }

    if (empty($_COOKIE[UsageConst::SESSION_NAME])) {
        $realSession = \sdk\UsageClient::instance()->createUUID(time() . '_');
        setcookie(UsageConst::SESSION_NAME, $realSession,
            0, '/',  "", false, true);
    }

    $headerNames = array_map(function ($value) {
        $name = explode(':', $value);
        if (empty($name[0])) {
            return "";
        }

        return trim(strtolower($name[0]));
    }, $headers);

    $key = array_search('location', $headerNames);
    if (!$key) {
        $code = 200;
        if (strpos($buffer, UsageConst::JUMP_FLAG) !== false) {
            $code = 302;
            $buffer = str_replace(UsageConst::JUMP_FLAG, '', $buffer);
        } else {
            if (strpos($buffer, UsageConst::JQUERY_FLAG) === false) {
                //
                if (!\sdk\UsageClient::instance()->isAjax()) {
                    $buffer = str_replace(
                        UsageConst::SDK_TAG,
                        UsageConst::JQUERY_SCRIPT . "\n" .
                        UsageConst::JQUERY_CONFLICT . "\n" .
                        UsageConst::SDK_TAG,
                        $buffer
                    );
                }
            }
            if (strpos($buffer, UsageConst::SDK_FLAG) === false) {
                $buffer = str_replace(
                    UsageConst::SDK_TAG,
                    UsageConst::SDK_SCRIPT . "\n" . UsageConst::SDK_TAG,
                    $buffer
                );
            }
            if (strpos($buffer, UsageConst::REQUEST_TIME) !== false) {
                $buffer = str_replace(
                    UsageConst::REQUEST_TIME,
                    str_replace('()', '(' . time() . ')', UsageConst::REQUEST_TIME),
                    $buffer
                );
            }
        }

        if ($code != 302) {
            /*
            if (!\sdk\UsageClient::instance()->isAjax()) {
                \sdk\UsageClient::instance()->log(
                    UsageConst::PRODUCT_BASE,
                    UsageConst::ACTION_PAGE_VIEW,
                    array(
                        'code' => $code,
                        'execute_time' => microtime(true) - $startTime
                    )
                );
            }
            */
        }

        return $buffer;
    }

    $found = $headers[$key];
    $url = trim(substr($found, strlen('location:')));
    //Location: https://dev.lexisnexis.com.cn/law/pwdremind.php?ssl=1&eng=0&url=
    //https://dev.lexisnexis.com.cn/law/pwdremind.php?ssl=1&eng=0&url=
    //header("Location: $url");

    $newLocation = \sdk\UsageClient::instance()->replaceUrl($url, $prePrid, $preCrid);
    header("Location: $newLocation");
    return $buffer;

}, 0, $bufferControl);
ob_implicit_flush(true);

output_reset_rewrite_vars();

//生成当前的crid
$crid = \sdk\UsageClient::instance()->createUUID();
//传过来有crid就把他当做prid
$prid = '';
if (!empty($preCrid)) {
    $prid = $preCrid;
}

if (\sdk\UsageClient::instance()->isAjax()) {
    $crid = $preCrid;
    $prid = $prePrid;
}

output_add_rewrite_var('crid', $crid);
output_add_rewrite_var('prid', $prid);

